var express = require('express');
const { get,select,removeCinemas, add, updateCinema, getOne, update,delImgs } = require("../api/cinemas.js");
const CinemasApi = require("../api/cinemas.js");
var router = express.Router();// 路由
const _ = require('lodash');


// 获取影院
router.get('/', async function (req, res, next) {
    // 解构当前页和每页显示条数
    const { curPage, eachPage } = req.query;
    const data = await get({ curPage, eachPage });

    res.send(data);

});

// 获取所有信息
router.get('/select', async function (req, res, next) {
    const slectInfo=req.query
    const data = await select(slectInfo);
   
    res.send(data)
});

// 查找影院信息
router.get('/:_id', async function (req, res, next) {
    // 做路径解析       /students/:_id
    const { _id } = req.params;
    const data = await getOne(_id);
    res.send(data);
});

// 删除影院
router.delete("/:_id", async function (req, res, next) {
    const { _id } = req.body;
    await CinemasApi.removeCinemas({ _id });
    res.send({ status: true });
});

// 新增影院
router.post('/', async function (req, res, next) {
    const { name, address, phone, price, distance, status } = req.body;
    // 获取截取的起始下标和结束下标
    const data = await add({ name, address, phone, price, distance, status, });
    // 发送截取后的数组数据
    res.send(data);
});

//修改影院 
router.put("/:_id", async function (req, res, next) {
    const newData = req.body;
    await CinemasApi.updateCinema(newData);
    res.send({ status: true });
})

// 上传图片
router.patch(`/:_id`, async function (req, res, next) {
    const { _id } = req.params;
    const data = req.body;  
    const result = await update({ _id }, data);
    res.send(result);
});

// 删除图片
router.delete(`/:_id/imgs/:url`, async function (req, res, next) {
    const { _id, url } = req.params;
    await delImgs({ _id, url });
    const data = await getOne(_id);
    res.send(data);

});

module.exports = router;